﻿using System;
using ChatCommon;
using ChatCommon.Packets;
using Lidgren.Network;
using NetBrick.Server;

namespace ChatServer.Handler
{
    public class ChatPacketHandler : PacketHandler
    {
        public ChatPacketHandler(Server server)
            : base(server)
        {
        }

        public override void Process(Object packetData, BasePeer sender)
        {
            var chatPeer = (ChatPeer) sender;
            var chatMessage = (ChatPacket.Request) packetData;
            var message = chatMessage.Message;
            if (!chatPeer.Authenticated || String.IsNullOrWhiteSpace(message)) return;
            var chatPacket = new ChatPacket.Event(chatPeer.Username, message);
            Log.LogMessage(ConsoleColor.Green, String.Format(" [Chat][{0}] ", chatPeer.Username), message);
            Server.BroadcastPacket(ChatPacketCode.ChatMessageEvent, chatPacket, NetDeliveryMethod.ReliableOrdered);
        }
    }
}